package Tools;

import java.util.ArrayList;

public class SimpleConsensus implements Consensus{

	@Override
	public ArrayList<int[]> calculate(String obj,OpinionBase profile) {
		ArrayList<int[]> ret=new ArrayList<int[]>();
		Graph gr= new Graph();
		ArrayList<InternalStatement> ir = new ArrayList<InternalStatement>();
		ArrayList<int[]> tmp= gr.getPossibleOpinions(new int[]{0,0,0,0});
		for(int i=0;i<tmp.size();i++){
			ir.add(new InternalStatement(tmp.get(i), 1));
		}
		ir.add(new InternalStatement(new int[]{3,0,0,0},1));
		ir.add(new InternalStatement(new int[]{0,3,0,0},1));
		ir.add(new InternalStatement(new int[]{0,0,3,0},1));
		ir.add(new InternalStatement(new int[]{0,0,0,3},1));
		double min=Double.MAX_VALUE;
		
		//DistanceFun dist = new CenterDistace();
		DistanceFun dist = new BotTopDistance();
		
		for(InternalStatement a:ir){
			double sum=0;
			for(PolarRepresentation pr:profile.getObjectOpinions(obj)){
				for(InternalStatement b:pr.ir){	
					sum+=Math.pow(b.weight*dist.calculate(PolarRepresentation.toInterval(a),PolarRepresentation.toInterval(b)),2);
				}
			}
			
			if(sum<min+0.00000001 && sum>min-0.00000001)
				ret.add(a.values);
			if(sum<min){
				min=sum;
				ret=new ArrayList<int[]>();
				ret.add(a.values);
			}
		}
		
		return ret;
	}

	@Override
	public int[] refine(ArrayList<int[]> cons) {
		int[] out={-2,-2,-2,-2};
		for(int i=0;i<cons.size();i++){
			for(int j=0;j<cons.get(i).length;j++){
				if(out[j]==-2||out[j]==cons.get(i)[j]){
					
					out[j]=cons.get(i)[j];
					
				}else{
					out[j]=-1;
				}
			}
		}
		for(int i=0;i<4;i++){
			if(out[i]==-1){
				out[i]=0;
			}
		}
		return out;
	}

}
